<?php
  require_once('includes/db.php');
  require_once('includes/header.php');
//pr1($_GET);
//pr1($user);

  $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;

//pr($_POST);

  if(isset($_POST['id'])) {
    $updatedRecords = updateById('users',$_POST,$id);
    if($updatedRecords == 1) {
      redirect('show_all_users.php');
    }
  }

  if($id != 0) {
    $user = fetchById('users',$id);
    if(!$user){redirect('show_all_users.php');}
    $isActive = $user['status']==1 ?'checked':'';
    $isInactive = $user['status']==0 ?'checked':'';
  } else {
    # back to show all users
    redirect('show_all_users.php');
  }
?>

    <div class="container">
    <form method="post" action="" class="form-horizontal" role="form">

<input type="hidden" name="id" value="<?=$user['id']?>" />

        <h2 class="form-signin-heading">Please edit the details below</h2>

        <div class="form-group">
          <label for="inputName" class="col-lg-2 control-label">Name</label>
          <div class="col-lg-10">
            <input value="<?=$user['name']?>" name="name" type="text" class="form-control" placeholder="My Name is">
          </div>
        </div>


        <div class="form-group">
          <label for="inputEmail1" class="col-lg-2 control-label">Email</label>
          <div class="col-lg-10">
            <input value="<?=$user['email']?>" name="email" type="email" class="form-control" id="inputEmail1" placeholder="Email">
          </div>
        </div>

        <div class="form-group">
          <label for="inputPhone" class="col-lg-2 control-label">Phone</label>
          <div class="col-lg-10">
            <input name="phone" value="<?=$user['phone']?>" type="text" class="form-control"  placeholder="Phone">
          </div>
        </div>

        <div class="form-group">
          <label class="col-lg-2 control-label">Status</label>
          <div class="col-lg-10">

            <div class="radio">
              <label><input type="radio" name="status" id="optionsRadios1" value="1" <?=$isActive?>>Active</label>
            </div>

            <div class="radio">
              <label><input type="radio" name="status" id="optionsRadios1" value="0" <?=$isInactive?> >Inactive</label>
            </div>

          </div>
        </div>

        <div class="form-group">
          <div class="col-lg-offset-2 col-lg-10">
            <input type="submit" class="btn btn-info btn-lg" value="UPDATE !!">
          </div>
        </div>
      </form>
<!------ Form End --------->

    </div> <!-- /container -->
<?php
  require_once('includes/footer.php');
?>
